package socket.httpserver.jhttp;

import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;

public class JHTTP {
    private static final Logger logger = Logger.getLogger(JHTTP.class.getCanonicalName());

    private static final int NUM_THREADS = 50;
    private static final String INDEX_FILE = "index.html";

    private final File rootDirectory;
    private final int port;

    public JHTTP(File rootDirectory, int port) throws IOException {
        if(!rootDirectory.isDirectory()) {
            throw new IOException(rootDirectory + " dose not exist as a directory");
        }
        this.rootDirectory = rootDirectory;
        this.port = port;
    }

    public void start() {
        ExecutorService pool = Executors.newFixedThreadPool(NUM_THREADS);
        try(ServerSocket server = new ServerSocket(port)) {
            logger.info("Accepting connections on port " + server.getLocalPort());
            logger.info("Document Root: " + rootDirectory);

            while(true) {
                Socket request = server.accept();
                RequstProcessor t = new RequstProcessor(rootDirectory, INDEX_FILE, request);
                pool.submit(t);
            }
        } catch (IOException e) {
            logger.log(Level.SEVERE, "Error accepting connection", e);
        }
    }
}
